# -*- py-indent-offset: 4 -*- 
# -*- tab-width: 8 -*- 
# -*- indent-tabs-mode: nil -*-
 
from etgui import *
import CmdNo,string
from PageLayout import *
from ListEditor import AbstrListEditor
class DictEditor(AbstrListEditor):
	"""A class that does simple list editing"""
	DoSwap=None
	def DoSetItems(self,dict):
		self.dict=dict
		self.items=dict.keys()
		self.items.sort()
		return self.items
	def DoInsert(self,i,key_value):
		key,value=key_value
		k=-1
		for k in range(len(self.items)):
			if self.items[k]>key:
				break
		else:
			k=k+1
		self.items.insert(k,key)
		self.dict[key]=value
		return k
	def DoMakeItem(self,key_value):
		try:
			key,value=key_value
		except (TypeError,ValueError):
			key=key_value
		return TextItem(str(key))
	def DoDelete(self,i):
		key=self.items[i]
		del self.dict[key]
		del self.items[i]
		return 1

class TestDictEditor(DictEditor):
	def __init__(self,what):
		self.what=what
		DictEditor.__init__(self)
	def _DoEdit(self,how,key_value):
		key,value=key_value
		m=PotGuiModel()
		m.SetValue("Name",key)
		retval=ShowSubDialog("%s %s" %(how,self.what),
			m,
			PotTextField("Name")
		)
		newkey=m.GetValue("Name")
		if newkey==key:
			return None
		if self.dict.has_key(newkey):
			ShowAlert(eAlertNote,"The name @B%s@B already exists!" %repr(newkey))
			return None
		if retval:
			return (newkey,value)
		else:
			return None
	def DoEdit(self,i):
		print self.dict
		key=self.items[i]
		value=self.dict[key]
		print repr((key,value))
		m=PotGuiModel()
		m.SetValue("Value",value)
		if type(value) == IntType:
			field=PotGuiIntField("Value",IntField(-1))
		else:
			field=PotTextField("Value")
		retval=ShowSubDialog("SetValue %s" %(self.what),
			m,
			field
		)
		if retval:
			self.dict[key]=m.GetValue("Value")
		print self.dict
	def DoNew(self,i):
		key_value=(self.FindUniqueName("NewItem"),"New Value")
		return self._DoEdit("Edit",key_value)
	def FindUniqueName(self,name):
		n=name
		i=1
		while self.dict.has_key(n):
			n="%s <%i>" %(name,i)
			i=i+1
		return n
	def DoCopy(self,i):
		key=self.items[i]
		value=self.dict[key]
		key_value=(self.FindUniqueName(key),value)
		return self._DoEdit("Copy",key_value)
	def DoRename(self,i):
		key=self.items[i]
		value=self.dict[key]
		key_value=(key,value)
		return self._DoEdit("Rename",key_value)

def MakeDictEditor(editor):
	items={
		"Test":1,
		"Michael":"scharf",
		"Bla": 9,
		"Edi":"ist im haus"
	}
	editor.SetItems(items)
	return MakeSectionLayout(
		MakeListEditorLayout(editor)
	)

def MakeDialog():
	panel=MakeTabSheetLayout(
		TabPage(
			"DictEditor",
			MakeDictEditor(DictEditor())
		),
		TabPage(
			"TestDictEditor",
			MakeDictEditor(TestDictEditor("DICT"))
		),
	)
	dialog=PotGuiDialog(
		PotGuiModel(),
		MakeDialogWithButtons(
			panel,
			ActionButton(CmdNo.cIdOk,"OK",default=1),
			ActionButton(CmdNo.cIdCancel,"Cancel"),
 
 
		),
		"Pot GUI",
		eWinDefault
	)
	return dialog

gDialog=None
def Show():
	global gDialog
	if not gDialog:
		gDialog=MakeDialog()
	gDialog.ShowUnderMouse()
